
public class GameLogic {
	
	public GameLogic(){
		
	}
	
	
	public boolean canPlaceMark(Square[][] board, int row, int col){
		return (board[row][col].getMark() == ' ');
	}
	
	
	public boolean checkWinner(Square[][] board){
		for(int row = 0; row < board[0].length; row++){
			for(int col = 0; col < board[0].length; col++){
				//checks horizontally
				if(this.checkNeighborMark(board, row, col, 1, 0, 1) || this.checkNeighborMark(board, row, col, -1, 0, 1)){
					System.out.println("Winner vertically");
					return true;
				}
				
				//checks vertically
				if(this.checkNeighborMark(board, row, col, 0, 1, 1) || this.checkNeighborMark(board, row, col, 0, -1, 1)){
					System.out.println("Winner horizontally");
					return true;
				}
				
				//checks diagonal
				if(this.checkNeighborMark(board, row, col, 1, 1, 1)){
					System.out.println("Winner diagonal");
					return true;
				}
				if(this.checkNeighborMark(board, row, col, 1, -1, 1)){
					System.out.println("Winner diagonal");
					return true;
				}
				if(this.checkNeighborMark(board, row, col, -1, 1, 1)){
					System.out.println("Winner diagonal");
					return true;
				}
				if(this.checkNeighborMark(board, row, col, -1, -1, 1)){
					System.out.println("Winner diagonal");
					return true;
				}
			}
		}
		
		return false;
	}
	
	
	private boolean checkNeighborMark(Square[][] board, int row, int col, int dRow, int dCol, int numberInContinuous){
		if(board[row][col].getMark() == ' '){
			return false;
		}
		if(row + dRow > board[0].length - 1 || row + dRow < 0){
			return false;
		}
		if(col + dCol > board[0].length - 1 || col + dCol < 0){
			return false;
		}
		if(board[row][col].getMark() == board[row + dRow][col + dCol].getMark()){
			if(numberInContinuous + 1 >= Constants.WINNER_LENGTH){
				return true;
			}
			else{
				return this.checkNeighborMark(board, row + dRow, col + dCol, dRow, dCol, numberInContinuous+1);
			}
		}
		return false;
	}

}
